﻿using MengLin.OOP.DesignPattern.WMS_LC.Implements;
using MengLin.OOP.DesignPattern.WMS_LC.Interface.Observer;
using MengLin.OOP.DesignPattern.WMS_LC.Interface.Subject;
using MengLin.OOP.DesignPattern.WMS_LC.Scheduler;
using MengLin.OOP.DesignPattern.WMS_LC.Scheduler.Implements;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MengLin.OOP.DesignPattern.WMS_LC.Interface
{
    /// <summary>
    /// 抽象的气压传感器
    /// </summary>
    public abstract class AbstractBarometricPressureSensor : ISubject
    {
        /// <summary>
        /// Ctor
        /// </summary>
        public AbstractBarometricPressureSensor()
        {
            BarometricPressureSensorAlarmListener barometricPressureAlarmListener = new BarometricPressureSensorAlarmListener(this);
            AlarmClock.WakeEvery(20, barometricPressureAlarmListener);
        }

        //最后1次读取到的大气压力
        private double lastReading;

        /// <summary>
        /// 读取大气压力
        /// </summary>
        /// <returns></returns>
        public abstract double Read();

        /// <summary>
        /// 读数是否发生了变化(注-模板方法模式：读取温度是可变的，检查逻辑固定的)
        /// </summary>
        public void MyCheck()
        {
            double barometricPressure = Read();//读取温度是可变的
            //检查固定的
            if (barometricPressure != lastReading)
            {
                lastReading = barometricPressure;
                //通知观察者
                NotifyObservers(barometricPressure);
            }
        }

        /// <summary>
        /// 添加观察者
        /// </summary>
        /// <param name="obj"></param>
        public abstract void AddObserver(IObserver obj);

        /// <summary>
        /// 移除观察者
        /// </summary>
        /// <param name="obj"></param>
        public abstract void UnRegisterObserver(IObserver obj);

        /// <summary>
        /// 通知观察者
        /// </summary>
        /// <param name="temperature"></param>
        public abstract void NotifyObservers(double temperature);
    }
}
